<?php
session_start();
include_once 'admin/connect.php';
include_once 'head.php';

// 开启错误显示，方便调试
ini_set('display_errors', 1);
error_reporting(E_ALL);

// 验证用户是否登录
if(!isset($_SESSION['loginadmin'])) {
    echo "<script>alert('隐私部分，请先登录后查看！');window.location.href='admin/login.php';</script>";
    exit;
}

// 验证session中的用户是否有效
$sql = "select * from login where user = '" . $_SESSION['loginadmin'] . "' ";
$loginresult = mysqli_query($connect, $sql);
if (!mysqli_num_rows($loginresult)) {
    // session无效，清除session并跳转
    session_unset();
    session_destroy();
    echo "<script>alert('请先登录后查看！');window.location.href='admin/login.php';</script>";
    exit;
}

// 验证通过，获取用户信息
$login = mysqli_fetch_array($loginresult);
$loveImg = "select * from loveImg order by id desc";
$resImg = mysqli_query($connect, $loveImg);
?>

<head>
    <link rel="stylesheet" href="Style/css/loveImg.css?LikeGirl=<?php echo $version ?>">
    <meta charset="utf-8" />
    <title><?php echo $text['title'] ?> — 珍贵相册</title>
    <style>
        .love_img img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }
        
        .love_img img.error {
            object-fit: contain;
            background: #f8f9fa;
        }
        
        .img_card {
            margin-bottom: 20px;
        }
        
        .words {
            padding: 10px;
            background: rgba(255,255,255,0.9);
        }
    </style>
</head>

<body>
    <div id="pjax-container">
        <h4 class="text-ce central">记录下你的最美瞬间</h4>
        <div class="row central">
            <?php
            while ($list = mysqli_fetch_array($resImg)) {
                $imgUrl = $list['imgUrl'];
                ?>
                <div class="img_card col-lg-4 col-md-6 col-sm-12 col-sm-x-12 <?php if ($text['Animation'] == "1") { ?>animated zoomIn delay-03s<?php } ?>">
                    <div class="love_img">
                        <?php if (strpos($imgUrl, 'http') === 0 || strpos($imgUrl, '//') === 0): ?>
                            <!-- 外部链接图片 -->
                            <img src="<?php echo htmlspecialchars($imgUrl) ?>" 
                                 alt="<?php echo htmlspecialchars($list['imgText']) ?>"
                                 data-description="<?php echo htmlspecialchars($list['imgDatd']) ?>"
                                 onerror="this.onerror=null; this.classList.add('error'); this.src='/Style/img/image-error.png';">
                        <?php else: ?>
                            <!-- 本地上传的图片 -->
                            <img src="<?php echo htmlspecialchars($imgUrl) ?>" 
                                 alt="<?php echo htmlspecialchars($list['imgText']) ?>"
                                 data-description="<?php echo htmlspecialchars($list['imgDatd']) ?>"
                                 onerror="this.onerror=null; this.classList.add('error'); this.src='/Style/img/image-error.png';">
                        <?php endif; ?>
                        <div class="words">
                            <i>Date：<?php echo htmlspecialchars($list['imgDatd']) ?></i>
                            <span><?php echo htmlspecialchars($list['imgText']) ?></span>
                        </div>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
    </div>

    <?php include_once 'footer.php'; ?>

    <script>
    $(function() {
        // 图片加载错误处理
        $('img').on('error', function() {
            $(this).addClass('error')
                   .attr('src', '/Style/img/image-error.png');
        });

        // 图片懒加载初始化
        $('img[data-funlazy]').each(function() {
            var $this = $(this);
            $this.attr('src', $this.data('funlazy'));
        });
    });
    </script>

    <?php if(isset($_SESSION['loginadmin'])): ?>
    <div style="display:none;">
        Debug Info:
        <?php
        while ($list = mysqli_fetch_array($resImg)) {
            echo "Image URL: " . htmlspecialchars($list['imgUrl']) . "<br>";
            echo "File exists: " . (file_exists($_SERVER['DOCUMENT_ROOT'] . $list['imgUrl']) ? 'Yes' : 'No') . "<br>";
        }
        ?>
    </div>
    <?php endif; ?>
</body>
</html>